package com.ruoyi.web.task;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fs.domain.FsCaseExpress;
import com.ruoyi.fs.domain.FsExpressInfo;
import com.ruoyi.fs.domain.FsWorkOrderPost;
import com.ruoyi.fs.service.IFsCaseExpressService;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.fs.service.IFsExpressInfoService;
import com.ruoyi.fs.service.IFsWorkOrderPostService;
import com.ruoyi.web.controller.tool.ExpressUtil;
import com.ruoyi.web.controller.tool.WuliuyunApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

@Component("expressTask")
public class ExpressTask {

    @Autowired
    private IFsCaseService fsCaseService;
    @Autowired
    private IFsExpressInfoService fsExpressInfoService;
    @Autowired
    private IFsCaseExpressService fsCaseExpressService;
    @Autowired
    private IFsWorkOrderPostService fsWorkOrderPostService;

    public void tbkdxx() {
        FsExpressInfo query = new FsExpressInfo();
        query.setYwzt(1);
        List<FsExpressInfo> expressInfoList = fsExpressInfoService.selectFsExpressInfoList(query);
        for (int i = 0; i < expressInfoList.size(); i++) {
            FsExpressInfo expressInfo = expressInfoList.get(i);
            String com = expressInfo.getCom();
            String kddh = expressInfo.getKddh();
            String phone = expressInfo.getDh();
            if (StringUtils.isNotBlank(com) && StringUtils.isNotBlank(kddh)) {
                String kdxx = WuliuyunApi.getPostResult(kddh, com, phone);
                JSONObject obj = JSONObject.parseObject(kdxx);
                boolean success = obj.getBoolean("success");
                if (success) {
                    String status = obj.getString("logisticsStatus");
                    String data = obj.getString("fullTraceDetail");
                    Date lastTime = null;
                    Date firstTime = null;
                    if (StringUtils.isNotBlank(data)) {
                        JSONArray ary = JSONArray.parseArray(data);
                        JSONObject de = (JSONObject) ary.get(0);
                        String time = de.getString("time");
                        lastTime = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", time);
                        expressInfo.setGxsj(lastTime);
                        de = (JSONObject) ary.get(ary.size() - 1);
                        time = de.getString("time");
                        firstTime = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", time);
                        expressInfo.setFjsj(firstTime);
                    }
                    if (status.contains("-")) {
                        status = status.split("-")[0];
                    }
                    expressInfo.setWlxx(data);
                    expressInfo.setKdzt(Integer.parseInt(status));
                    if (status.startsWith("10")) {
                        expressInfo.setYwzt(2);
                        expressInfo.setQssj(lastTime);
                    }
                    fsExpressInfoService.updateFsExpressInfo(expressInfo);
                }
            }
        }
    }
}
